package com.littleken.controller;

import com.littleken.entity.User;
import com.littleken.service.UserServiceImpl;
import com.littleken.service.impl.UserService;
import com.littleken.utils.JsonData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/userlogin")
public class UserController extends HttpServlet {
    UserService service = new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
//        System.out.println(username);
//        System.out.println(password);
        User user = service.getUserByUsername(username);
        System.out.println(user);
        if(user==null){
            System.out.println("没有这用户");
            req.getSession().setAttribute("msg","没有这个用户");
            resp.sendRedirect(req.getContextPath()+"/login.jsp");
           return;
        }else if (!user.getPassword().equals(password)){
            System.out.println("密码错");
            req.getSession().setAttribute("msg","用户或密码错");
            resp.sendRedirect(req.getContextPath()+"/login.jsp");
            return;
        }
        req.getSession().setAttribute("user",user);
        resp.setContentType("application/json;charset=UTF-8");
        resp.sendRedirect(req.getContextPath()+"/home.jsp");
    }
}
